//package com.daxt.controller.filemanager;
//
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.FileNotFoundException;
//import java.io.InputStream;
//import java.time.LocalDateTime;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//
//import javax.servlet.http.HttpServletRequest;
//
//import com.daxt.base.common.util.RedisUtil;
//import com.daxt.model.dic.SystemConfigType;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.transaction.annotation.Transactional;
//import org.springframework.util.StringUtils;
//import org.springframework.web.bind.annotation.DeleteMapping;
//import org.springframework.web.bind.annotation.GetMapping;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.PostMapping;
//import org.springframework.web.bind.annotation.PutMapping;
//import org.springframework.web.bind.annotation.RequestBody;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.RestController;
//
//import com.alibaba.fastjson.JSON;
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.baomidou.mybatisplus.core.metadata.IPage;
//import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
//import com.daxt.common.result.Result;
//import com.daxt.common.result.ResultCode;
//import com.daxt.common.result.ResultUtil;
//import com.daxt.mapper.archives.ArchivesDocFileMapper;
//import com.daxt.mapper.archives.ArchivesDocMapper;
//import com.daxt.mapper.archives.ArchivesTypeDocMapper;
//import com.daxt.mapper.sys.ArchivesFondsMapper;
//import com.daxt.mapper.sys.ArchivesTypeMapper;
//import com.daxt.model.service.archives.param.GenCategoryNum;
//import com.daxt.model.service.archives.result.ArchivesCols;
//import com.daxt.model.service.archives.result.ArchivesDetails;
//import com.daxt.model.service.archives.result.ArchivesList;
//import com.daxt.model.service.archives.vo.ArchivesDoc;
//import com.daxt.model.service.archives.vo.ArchivesDocFile;
//import com.daxt.model.service.archives.vo.ArchivesFonds;
//import com.daxt.model.service.archives.vo.ArchivesType;
//import com.daxt.model.service.archives.vo.ArchivesTypeDoc;
//import com.daxt.model.xml.ApprovalBaseInfo;
//import com.daxt.model.xml.ProcessBaseInfo;
//import com.daxt.service.DocService;
//import com.daxt.service.FondsListService;
//import com.daxt.service.OperationLogService;
//import com.daxt.utils.FileGenUtil;
//import com.thoughtworks.xstream.XStream;
//import com.thoughtworks.xstream.io.xml.DomDriver;
//import com.thoughtworks.xstream.mapper.MapperWrapper;
//
//import cn.hutool.core.io.FileBaseUtil;
//import cn.hutool.core.io.IoUtil;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiOperation;
//import io.swagger.annotations.ApiParam;
//
//@Api(tags = "文件整理")
//@RestController
//@RequestMapping("/accept/doc")
//public class AcceptDocController {
//
//	@Autowired
//	private ArchivesDocMapper archivesDocMapper;
//	@Autowired
//	private ArchivesTypeMapper archivesTypeMapper;
//	@Autowired
//	private ArchivesTypeDocMapper archivesTypeDocMapper;
//	@Autowired
//	private ArchivesFondsMapper archivesFondsMapper;
//	@Autowired
//	private ArchivesDocFileMapper archivesDocFileMapper;
//	@Autowired
//	private DocService docService;
//	@Autowired
//	private OperationLogService operationLogService;
//	@Autowired
//	private FondsListService fondsListService;
////	@Value(value = "${systemConfig.createNum}")
////	private Integer createNum;
////	@Value(value = "${systemConfig.createFileNum}")
////	private Integer createFileNum;
//
//
//
//
//	@ApiOperation(value = "保存文件", notes = "")
//	@PostMapping("/doc/{fondsId}/{typeId}")
//	public Result<String> docSave(@PathVariable("fondsId")String fondId,@PathVariable("typeId")String typeId,
//			@RequestBody ArchivesDoc archivesDoc,HttpServletRequest request) {
//
//		QueryWrapper<ArchivesFonds> queryWrapperFondsName=new QueryWrapper<ArchivesFonds>();
//		queryWrapperFondsName.eq("fondsId", archivesDoc.getFondsId());
//		queryWrapperFondsName.select("fondsName");
//		List<ArchivesFonds> selectList = archivesFondsMapper.selectList(queryWrapperFondsName);
//		if(selectList!=null&&selectList.size()>0) {
//			String fondsName = selectList.get(0).getFondsName();
//			archivesDoc.setFondsName(fondsName);
//		}else {
//			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "全宗号不正确！！！",null);
//		}
//
//		String createFileNum = RedisUtil.getSysConfig(SystemConfigType.ITEM_NUM_LENGTH);
//		if(StringUtils.isEmpty(createFileNum))return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "缺少系统配置key：ITEM_NUM_LENGTH",null);
//
//		String typeNum = archivesDoc.getTypeNum();
//		String fondsId = archivesDoc.getFondsId();
//		QueryWrapper<ArchivesType> queryWrapper = new QueryWrapper<>();
//		queryWrapper.eq("typeNum", typeNum);
//		queryWrapper.eq("fondsId", fondsId);
//		List<ArchivesType> typeList = archivesTypeMapper.selectList(queryWrapper);
//		//ArchivesDoc dossier=null;
//		if (typeList == null || typeList.size() == 0) {
//			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "门类不正确", null);
//		}
//		String model = typeList.get(0).getModel();
//		if ("0".equals(model)) {
//			//archivesDoc.setDossierId(1L);
//			ArchivesType archivesType = typeList.get(0);
//			Map parseObject = JSON.parseObject(JSON.toJSONString(archivesDoc), Map.class);
//			List<GenCategoryNum> column=fondsListService.genCategoryList(archivesType.getGenClassNum(),archivesType.getGenClassType(),parseObject);
//			//String categoryNum = fondsListService.genCategoryNum(fondsId, typeNum, column,archivesType.getGenClassType());
//			String categoryNum = fondsListService.genCategoryNumNew(column,archivesType.getGenClassType(),parseObject);
//			if (StringUtils.isEmpty(categoryNum))
//				return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "类目号拼接列不能为空，请检查类目生成规则！！！", null);
//			archivesDoc.setCategoryNum(categoryNum);
//			parseObject = JSON.parseObject(JSON.toJSONString(archivesDoc), Map.class);
//			String genArchivalNum=fondsListService.genArchivalNum(archivesType.getGenArchivalNum(),archivesType.getGenType(),parseObject);
//			if (StringUtils.isEmpty(genArchivalNum))
//				return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "档号拼接列不能为空，请检查类目生成规则！！！", null);
//			archivesDoc.setArchivalNum(genArchivalNum);
//		} else {
//
//			QueryWrapper<ArchivesDoc> queryWrapperDossierCount=new QueryWrapper<ArchivesDoc>();
//			queryWrapperDossierCount.eq("id", archivesDoc.getDossierId());
//			Integer dossier = archivesDocMapper.selectCount(queryWrapperDossierCount);// ById(archivesDoc.getDossierId());
//			if (dossier == null||dossier.intValue()==0) {
//				return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "案卷不存在", null);
//			}//else {
//				//archivesDoc.setArchivesCode(a)
//				//archivesDoc.setCategoryNum(dossier.getCategoryNum());
//				//archivesDoc.setArchivalNum(dossier.getArchivalNum());
//				//archivesDoc.setDossierNum(dossier.getDossierNum());
//				//archivesDoc.setFondsId(dossier.getFondsId());
//				//archivesDoc.setTypeNum(dossier.getTypeNum());
//			//}
//		}
//
//
//		Page<ArchivesDoc> page = new Page<ArchivesDoc>(1, 1);
//		QueryWrapper<ArchivesDoc> queryWrapperDoc = new QueryWrapper<ArchivesDoc>();
//		//queryWrapperDoc.eq("typeNum", typeNum);
//		queryWrapperDoc.eq("fondsId", fondsId);
//		queryWrapperDoc.eq("fileType", "doc");
//		if ("0".equals(model)) {
//			queryWrapperDoc.eq("categoryNum",archivesDoc.getCategoryNum());
//			//queryWrapperDoc.eq("archivalYear",archivesDoc.getArchivalYear());
//			queryWrapperDoc.eq("dossierId", "0");
//		}else {
//			queryWrapperDoc.eq("dossierId", archivesDoc.getDossierId());
//		}
//		Integer createNum = Integer.parseInt(RedisUtil.getSysConfig(SystemConfigType.CREATE_NUM));
//		queryWrapperDoc.isNotNull("itemNum");
//		queryWrapperDoc.last("and LENGTH(itemNum)="+("0".equals(model)?createFileNum:createNum)+ " ORDER BY itemNum desc ");
//		//queryWrapperDoc.orderByDesc("itemNum");
//		IPage<ArchivesDoc> selectPage = archivesDocMapper.selectPage(page, queryWrapperDoc);
//		String itemNum = "0";
//		if (selectPage != null && selectPage.getRecords().size() > 0) {
//			String itemNum2 = selectPage.getRecords().get(0).getItemNum();
//			itemNum =FileGenUtil.numGen(itemNum2, createFileNum==null?1:Integer.parseInt(createFileNum));
//		}else {
//			itemNum =FileGenUtil.numGen(itemNum, createFileNum==null?1:Integer.parseInt(createFileNum));
//		}
//		archivesDoc.setItemNum(itemNum);
//
//
//		archivesDoc.setFileType("doc");
//		archivesDoc.setCreateTime(LocalDateTime.now());
//		archivesDoc.setDocFrom("1");
//		Result<String> docSave = docService.docSave(archivesDoc);
//		operationLogService.saveLog("2", "添加件："+JSON.toJSONString(archivesDoc)+"件"+(docSave.getCode().equals(ResultCode.RESULT_SUCCESS.getCode())?"成功":"失败"), request);
//		return docSave;
//	}
//
//	@ApiOperation(value = "保存散文件", notes = "")
//	@PostMapping("/docScattered")
//	public Result<String> docScattered(@RequestBody ArchivesDoc archivesDoc) {
//		String typeNum = archivesDoc.getTypeNum();
//		String fondsId = archivesDoc.getFondsId();
//		QueryWrapper<ArchivesType> queryWrapper = new QueryWrapper<>();
//		queryWrapper.eq("typeNum", typeNum);
//		queryWrapper.eq("fondsId", fondsId);
//		List<ArchivesType> typeList = archivesTypeMapper.selectList(queryWrapper);
//		if (typeList == null || typeList.size() == 0) {
//			return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "门类不正确", null);
//		}
//		String model = typeList.get(0).getModel();
//		if ("0".equals(model)) {
//			archivesDoc.setDossierId(1L);
//			ArchivesType archivesType = typeList.get(0);
//			Map parseObject = JSON.parseObject(JSON.toJSONString(archivesDoc), Map.class);
//			List<GenCategoryNum> column=fondsListService.genCategoryList(archivesType.getGenClassNum(),archivesType.getGenClassType(),parseObject);
//			//String categoryNum = fondsListService.genCategoryNum(fondsId, typeNum, column,archivesType.getGenClassType());
//			String categoryNum = fondsListService.genCategoryNumNew(column,archivesType.getGenClassType(),parseObject);
//			if (StringUtils.isEmpty(categoryNum))
//				return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "类目号拼接列不能为空，请检查类目生成规则！！！", null);
//			archivesDoc.setCategoryNum(categoryNum);
//			String genArchivalNum=fondsListService.genArchivalNum(archivesType.getGenArchivalNum(),archivesType.getGenType(),parseObject);
//			if (StringUtils.isEmpty(genArchivalNum))
//				return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "档号拼接列不能为空，请检查类目生成规则！！！", null);
//			archivesDoc.setArchivalNum(genArchivalNum);
//		} else {
//			//dossier = archivesDocMapper.selectById(archivesDoc.getDossierId());
//			//if (dossier == null) {
//			//	return ResultUtil.data(ResultCode.RESULT_ERROR.getCode(), "案卷不存在", null);
//			//}else {
//			//	archivesDoc.setCategoryNum(dossier.getCategoryNum());
//			//	archivesDoc.setArchivalNum(dossier.getArchivalNum());
//			//	archivesDoc.setDossierNum(dossier.getDossierNum());
//			//	archivesDoc.setFondsId(dossier.getFondsId());
//			//	archivesDoc.setTypeNum(dossier.getTypeNum());
//			//}
//		}
//
//
//
//		archivesDoc.setDossierId(0L);
//		archivesDoc.setState("0");
//		archivesDoc.setFileType("scattered");
//		archivesDoc.setDocFrom("1");
//		archivesDoc.setCreateTime(LocalDateTime.now());
//		return docService.docSave(archivesDoc);
//	}
//
//
//}
